package net.monare.mifss.admin.ui;

import com.vaadin.ui.*;
import net.monare.mifss.MifssAdminMainUI;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.evn.AddStorageListener;
import net.monare.mifss.evn.LogoutListener;
import net.monare.mifss.evn.ConfigureStorageListener;

import com.vaadin.server.ThemeResource;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.themes.Runo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

public class AdminWorkPanel extends VerticalLayout {

	private static final long serialVersionUID = 8049524900962921928L;

	public AdminWorkPanel() {

		setSizeFull();
		setSpacing(true);

		HorizontalLayout titleLayout = new HorizontalLayout();
		titleLayout.setSizeFull();
		titleLayout.setMargin(true);
        titleLayout.setSpacing(true);

		Label horizontalSeparator1 = new Label("<hr/>", ContentMode.HTML);
		Label horizontalSeparator2 = new Label("<hr/>", ContentMode.HTML);

		// load messages for UI
		String adminTitleLabel = MifssMessages.getMessage("title.admin.page");
		String buttonLogoutText = MifssMessages.getMessage("button.logout");
		String buttonNewStorageText = MifssMessages.getMessage("button.new.storage");
		String buttonConfigureStorageText = MifssMessages.getMessage("button.configure.storage");
		String buttonRemoveStorageText = MifssMessages.getMessage("button.remove.storage");
        String currentUserLabelText = MifssMessages.getMessage("label.current.user.name");



		// add title label
		Label title = new Label(adminTitleLabel);
		title.setStyleName(Runo.LABEL_H1);
		titleLayout.addComponent(title);

		// add logout button
		NativeButton logout = new NativeButton(buttonLogoutText);
		LogoutListener logoutListener = new LogoutListener();
		logout.addClickListener(logoutListener);


        Label labelUser = new Label();

        String userName = ((MifssAdminMainUI)UI.getCurrent()).getMifssHolder().getSecurityContext().getAuthentication().getName();

        labelUser.setValue(currentUserLabelText + " " + userName);


        titleLayout.addComponent(logout);
        titleLayout.addComponent(labelUser);

		titleLayout.setComponentAlignment(title, Alignment.MIDDLE_LEFT);
		titleLayout.setComponentAlignment(logout, Alignment.MIDDLE_RIGHT);
        titleLayout.setComponentAlignment(labelUser, Alignment.MIDDLE_RIGHT);

		// add logout button
		NativeButton addStorageButton = new NativeButton(buttonNewStorageText);
		addStorageButton.addClickListener(new AddStorageListener());
		
		Image addStorageImage = new Image(null, new ThemeResource("img/mifss_storage_add.png"));

		NativeButton configureStorageButton = new NativeButton(buttonConfigureStorageText);
		configureStorageButton.addClickListener(new ConfigureStorageListener());
		
		Image configureStorageImage = new Image(null, new ThemeResource("img/mifss_storage_configure.png"));

		NativeButton removeStorageButton = new NativeButton(buttonRemoveStorageText);
		Image removeStorageImage = new Image(null, new ThemeResource("img/mifss_storage_remove.png"));

		// add grid layout
		GridLayout gridLayout = new GridLayout(3, 2);
		gridLayout.setMargin(true);

		gridLayout.setWidth("700px");
		gridLayout.setHeight("350px");

		gridLayout.addComponent(addStorageImage, 0, 0);
		gridLayout.addComponent(addStorageButton, 0, 1);
		gridLayout.setComponentAlignment(addStorageImage, Alignment.MIDDLE_CENTER);
		gridLayout.setComponentAlignment(addStorageButton, Alignment.TOP_CENTER);

		gridLayout.addComponent(configureStorageImage, 1, 0);
		gridLayout.addComponent(configureStorageButton, 1, 1);
		gridLayout.setComponentAlignment(configureStorageImage, Alignment.MIDDLE_CENTER);
		gridLayout.setComponentAlignment(configureStorageButton, Alignment.TOP_CENTER);

		gridLayout.addComponent(removeStorageImage, 2, 0);
		gridLayout.addComponent(removeStorageButton, 2, 1);
		gridLayout.setComponentAlignment(removeStorageImage, Alignment.MIDDLE_CENTER);
		gridLayout.setComponentAlignment(removeStorageButton, Alignment.TOP_CENTER);

		addComponent(titleLayout);
		addComponent(horizontalSeparator1);
		addComponent(gridLayout);
		setComponentAlignment(gridLayout, Alignment.MIDDLE_CENTER);
		addComponent(horizontalSeparator2);

	}
}
